package com.wk.warehouse.service.impl;

import java.util.HashMap;
import java.util.List;
import com.wk.warehouse.entity.ProductCategory;
import com.wk.warehouse.mapper.ProductCategoryMapper;
import com.wk.warehouse.page.Page;
import com.wk.warehouse.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ProductCategoryServiceImp implements ProductCategoryService {

	@Autowired
	private ProductCategoryMapper productCategoryMapper;

	/**
	 * 查询所有分类，树形结构
	 */
	@Override
	public List<ProductCategory> findTypeTree() {
		List<ProductCategory> firstTypeList = productCategoryMapper.findFirstTypeList();
		if(firstTypeList!=null && firstTypeList.size()>0){
			for(ProductCategory firstType: firstTypeList){
				int firstTypeId = firstType.getTypeId();
				// 查询一级分类的子分类
				List<ProductCategory> secondTypeList = productCategoryMapper.findChildTypeList(firstTypeId);
				firstType.setChildProductCategory(secondTypeList);
			}
		}
		return firstTypeList;
	}

	/**
	 * 按照父id查询分类数据
	 */
	@Override
	public List<ProductCategory> findChildType(int parentId) {
		return productCategoryMapper.findChildTypeList(parentId);
	}

	/**
	 * 通过分类编号查询分类
	 */
	@Override
	public ProductCategory queryByTypeCode(String typeCode) {
		return productCategoryMapper.queryByTypeCode(typeCode);
	}

	/**
	 * 保存新分类
	 */
	@Override
	public int saveType(ProductCategory productCategory) {
		return productCategoryMapper.saveType(productCategory);
	}

	/**
	 * 更新分类信息
	 */
	@Override
	public int updateType(ProductCategory productCategory) {
		return productCategoryMapper.updateType(productCategory);
	}

	/**
	 * 通过分类id 查找商品
	 */
	@Override
	public ProductCategory queryByTypeId(int typeId) {
		return productCategoryMapper.queryByTypeId(typeId);
	}

	/**
	 * 删除商品分类 - 如果有子分类，则将子分类一起删除
	 */
	@Override
	public int deleteType(int typeId) {
		int updateRows = productCategoryMapper.deleteType(typeId);
		List<ProductCategory> childList = productCategoryMapper.findChildTypeList(typeId);
		if(childList!=null && childList.size()>0){
			for(ProductCategory productCategory: childList){
				productCategoryMapper.deleteType(productCategory.getTypeId());
				updateRows++;
			}
		}
		return updateRows;
	}
}
